"""
部门模型
"""
from typing import Optional, List
from pydantic import BaseModel, Field
from datetime import datetime
from bson import ObjectId
from app.models.base import PyObjectId

class DepartmentBase(BaseModel):
    name: str = Field(..., description="部门名称")
    parent_id: Optional[str] = Field(None, description="父部门ID")
    manager_id: Optional[str] = Field(None, description="部门负责人ID")
    order: int = Field(0, description="排序")
    description: Optional[str] = Field(None, description="部门描述")

class DepartmentCreate(DepartmentBase):
    pass

class DepartmentUpdate(BaseModel):
    name: Optional[str] = Field(None, description="部门名称")
    parent_id: Optional[str] = Field(None, description="父部门ID")
    manager_id: Optional[str] = Field(None, description="部门负责人ID")
    order: Optional[int] = Field(None, description="排序")
    description: Optional[str] = Field(None, description="部门描述")

class DepartmentInDB(DepartmentBase):
    id: PyObjectId = Field(default_factory=PyObjectId, alias="_id")
    created_at: datetime = Field(default_factory=datetime.utcnow)
    updated_at: datetime = Field(default_factory=datetime.utcnow)

    class Config:
        populate_by_name = True
        arbitrary_types_allowed = True
        json_encoders = {ObjectId: str}

class Department(DepartmentInDB):
    """部门模型"""
    children: Optional[List['Department']] = Field(default_factory=list, description="子部门")
